/*
 * This file is part of "IdeWiki" project.
 *
 * Copyright (c) 2008 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/idewiki for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */

package ws.ebel.macro;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Writer;
import java.text.MessageFormat;
import java.util.Properties;

import org.snipsnap.render.macro.SnipMacro;
import org.snipsnap.render.macro.parameter.SnipMacroParameter;
import org.snipsnap.snip.Snip;
import org.snipsnap.snip.SnipSpaceFactory;

/**
 * IdeWikiMacro
 * 
 * @author ebel_h1
 * @created 08.12.2006
 */
public class IdeWikiMacro extends SnipMacro{
	
    /** The param description. */
    private String paramDescription[] = { "1: p=project name", "2: c=full qualified class name" , "3: cvsRoot=CVS root path (optional for Web CVS linking)", "4: srcFolder=source folder of the project (optional for Web CVS linking)"};

    /**
     * The Constructor.
     */
    public IdeWikiMacro() {
    }

    /**
     * Gets the name.
     * 
     * @return the name
     */
    public String getName() {
        return "ide";
    }

    /**
     * Gets the description.
     * 
     * @return the description
     */
    public String getDescription() {
        return "Create a link to a java type in the eclipse ide or web cvs.";
    }

    /**
     * Gets the param description.
     * 
     * @return the param description
     */
    public String[] getParamDescription() {
        return paramDescription;
    }

    /**
     * Execute.
     * 
     * @param params the params
     * @param writer the writer
     * 
     * @throws IOException the IO exception
     * @throws IllegalArgumentException the illegal argument exception
     */
    public void execute(Writer writer, SnipMacroParameter params) throws IllegalArgumentException, IOException {
        // no parameter
        if (params == null && params.getLength() < 2) {
            writer.write("<b><font style=\"font-color:red;\">IDE Macro Error: No parameter!</font></b><br/>");
        } else {

            // check config snip
            Snip macroConfigSnip = SnipSpaceFactory.getInstance().load("SnipSnap/config/ideMacroConfig");
            if (macroConfigSnip == null || macroConfigSnip.getContent() == null) {
                writer.write("<b><font style=\"font-color:red;\">IDE Macro Error: macro is not configured! Please create [SnipSnap/config/ideMacroConfig]</font></b><br/>");
            } else {
                String projectName = params.get("p");
                String resourceName = params.get("c");
                String cvsRoot = params.get("cvsRoot");
                String srcFolder = params.get("srcFolder");
                Properties properties = new Properties();
                byte[] myStrBytes = macroConfigSnip.getContent().getBytes();
                properties.load(new ByteArrayInputStream(myStrBytes));

                if (projectName != null && resourceName != null && cvsRoot != null && srcFolder != null) {
                    String classPath = projectName + srcFolder + resourceName.replace('.', '/') + ".java";

                    String webCvsUrl = properties.getProperty(cvsRoot);
                    // check properties
                    if (webCvsUrl != null) {
                        String[] placeholder = { classPath };
                        String ideLink = MessageFormat.format(webCvsUrl, placeholder);
                        // add properties for eclipse
                        ideLink = ideLink + "&p=" + projectName + "&c=" + resourceName;
                        writeIdeLink(writer, projectName, resourceName, ideLink, properties);
                    } else {
                        writer.write("<b><font style=\"font-color:red;\">IDE Macro Error: macro is not correct configured! Please add cvs root property for '"
                                + cvsRoot + "' [SnipSnap/config/ideMacroConfig]</font></b><br/>");
                    }
                } else if (projectName != null && resourceName != null && cvsRoot == null && srcFolder == null) {
                    String ideLink = "space/IdeMacro?p=" + projectName + "&c=" + resourceName;
                    writeIdeLink(writer, projectName, resourceName, ideLink, properties);
                } else {
                    writer.write("<b><font style=\"font-color:red;\">IDE Macro Error: Wrong parameter!</font></b><br/>");
                }
            }
        }
    }

    /**
     * Write ide link.
     * 
     * @param writer the writer
     * @param ideLink the ide link
     * @param projectName the project name
     * @param resourceName the resource name
     * 
     * @throws IOException the IO exception
     */
    private void writeIdeLink(Writer writer, String projectName, String resourceName, String ideLink, Properties aProp) throws IOException {
        String className = getClassName(resourceName);
        String title = projectName + ":" + resourceName;
        writer.write("<span class=\"nobr\">");
        
        String showProjectNameFlag = aProp.getProperty("showProjectName");
        
        if (className != null) {
            if(showProjectNameFlag != null && showProjectNameFlag.equals("true")){
                writer.write("<a class=\"ideMacroLink\" href=\"" + ideLink + "\" title=\"" + title + "\">" + projectName + " : " + className + "</a>");
            } else {
                writer.write("<a class=\"ideMacroLink\" href=\"" + ideLink + "\" title=\"" + title + "\">" + className + "</a>");
            }
        } else {
            if(showProjectNameFlag != null && showProjectNameFlag.equals("true")){
                writer.write("<a class=\"ideMacroLink\" href=\"" + ideLink + "\" title=\"" + title + "\">" + projectName + " : " + resourceName + "</a>");
            } else {
                writer.write("<a class=\"ideMacroLink\" href=\"" + ideLink + "\" title=\"" + title + "\">" + resourceName + "</a>");
            }
        }
        writer.write("</span>");
    }

    /**
     * Gets the class name.
     * 
     * @param resourceName the resource name
     * 
     * @return the class name
     */
    private String getClassName(String resourceName) {
        String result = null;
        try {
            String temp = resourceName.substring(resourceName.lastIndexOf(".") + 1);
            char first = temp.substring(0, 1).charAt(0);
            if (temp.length() > 1 && Character.isUpperCase(first))
                result = temp;
        } catch (Exception exception) {
        }
        return result;
    }
}

/*
 * --------------------------------------------------------------------------
 * $Log: $
 * --------------------------------------------------------------------------
 */